<template>
    <div class="p-2">
        <transition :enter-active-class="proxy?.animate.searchAnimate.enter"
            :leave-active-class="proxy?.animate.searchAnimate.leave">
            <div v-show="showSearch" class="mb-[10px]">
                <el-card shadow="hover">
                    <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="120px">
                        <el-form-item label="序列号SN" prop="serialNumberSn">
                            <el-input v-model="queryParams.serialNumberSn" placeholder="请输入序列号SN" clearable
                                @keyup.enter="handleQuery" />
                        </el-form-item>
                        <el-form-item label="密钥KEY" prop="secretKey">
                            <el-input v-model="queryParams.secretKey" placeholder="请输入密钥KEY" clearable
                                @keyup.enter="handleQuery" />
                        </el-form-item>
                        <el-form-item label="打印机名称" prop="printerName">
                            <el-input v-model="queryParams.printerName" placeholder="请输入打印机名称" clearable
                                @keyup.enter="handleQuery" />
                        </el-form-item>
                        <el-form-item label="打印机型号" prop="printerModel">
                            <el-input v-model="queryParams.printerModel" placeholder="请输入打印机型号" clearable
                                @keyup.enter="handleQuery" />
                        </el-form-item>
                        <el-form-item label="授权用户" prop="nickName">
                            <el-input v-model="queryParams.nickName" placeholder="请输入授权用户" clearable
                                @keyup.enter="handleQuery" />
                        </el-form-item>
                        <el-form-item label="打印机品牌" prop="printerBrand">
                            <el-input v-model="queryParams.printerBrand" placeholder="请输入打印机品牌" clearable
                                @keyup.enter="handleQuery" />
                        </el-form-item>
                        <el-form-item label="生产商" prop="manufacturer">
                            <el-input v-model="queryParams.manufacturer" placeholder="请输入生产商" clearable
                                @keyup.enter="handleQuery" />
                        </el-form-item>
                        <el-form-item label="厂家联系人" prop="manufacturerContact">
                            <el-input v-model="queryParams.manufacturerContact" placeholder="请输入厂家联系人" clearable
                                @keyup.enter="handleQuery" />
                        </el-form-item>
                        <el-form-item label="厂家电话" prop="manufacturerPhone">
                            <el-input v-model="queryParams.manufacturerPhone" placeholder="请输入厂家电话" clearable
                                @keyup.enter="handleQuery" />
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                        </el-form-item>
                    </el-form>
                </el-card>
            </div>
        </transition>

        <el-card shadow="never">
            <template #header>
                <el-row :gutter="10" class="mb8">
                    <el-col :span="1.5">
                        <el-button type="primary" icon="Plus" @click="handleAdd"
                            v-hasPermi="['order:printerInfo:add']">新增</el-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button type="success" icon="Edit" :disabled="single" @click="handleUpdate()"
                            v-hasPermi="['order:printerInfo:edit']">修改</el-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button type="danger" icon="Delete" :disabled="multiple" @click="handleDelete()"
                            v-hasPermi="['order:printerInfo:remove']">删除</el-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button type="warning" icon="Download" @click="handleExport"
                            v-hasPermi="['order:printerInfo:export']">导出</el-button>
                    </el-col>
                    <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
                </el-row>
            </template>

            <el-table v-loading="loading" :data="printerInfoList" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55" align="center" />
                <el-table-column label="打印机名称" align="center" prop="printerName" />
                <!-- 打印机类型 -->
                <el-table-column label="打印机类型" align="center" prop="printerType">
                    <template #default="scope">
                        <span v-if="scope.row.printerType == 1">WIFI</span>
                        <span v-else-if="scope.row.printerType == 2">4G</span>
                    </template>
                </el-table-column>
                <el-table-column label="打印机卡号" align="center" prop="printerCard" />
                <el-table-column label="序列号SN" align="center" prop="serialNumberSn" />
                <el-table-column label="密钥KEY" align="center" prop="secretKey" />
                <el-table-column label="打印机型号" align="center" prop="printerModel" />
                <el-table-column label="授权用户" align="center" prop="nickName" />
                <el-table-column label="打印机品牌" align="center" prop="printerBrand" />
                <el-table-column label="生产商" align="center" prop="manufacturer" />
                <el-table-column label="厂家联系人" align="center" prop="manufacturerContact" />
                <el-table-column label="厂家电话" align="center" prop="manufacturerPhone" />
                <el-table-column label="启用状态" align="center" prop="activationStatus">
                    <template #default="scope">
                        <!-- {{ scope.row.activationStatus }} -->
                        <el-switch v-model="scope.row.activationStatus" active-value="0" inactive-value="1"
                            @change="statusChange(scope.row)">
                        </el-switch>
                    </template>
                </el-table-column>
                <el-table-column label="心跳" align="center">
                    <template #default="scope">
                        <img v-if="parseJson(scope.row.heartStatus) === '在线，工作状态正常。'"
                            src="@/assets/images/heart_active.svg" />
                        <img v-else src="@/assets/images/heart_inactive.svg" />
                    </template>
                </el-table-column>
                <el-table-column label="备注" align="center" prop="remark" />
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                    <template #default="scope">
                        <el-tooltip content="修改" placement="top">
                            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                                v-hasPermi="['order:printerInfo:edit']"></el-button>
                        </el-tooltip>
                        <el-tooltip content="删除" placement="top">
                            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                                v-hasPermi="['order:printerInfo:remove']"></el-button>
                        </el-tooltip>
                    </template>
                </el-table-column>
            </el-table>

            <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
                v-model:limit="queryParams.pageSize" @pagination="getList" />
        </el-card>
        <!-- 添加或修改打印机信息对话框 -->
        <el-dialog :title="dialog.title" v-model="dialog.visible" width="800px" append-to-body>
            <el-form ref="printerInfoFormRef" label-width="150px" :inline="false" :model="form" :rules="rules">
                <el-row :gutter="20">
                    <el-col :span="12" :offset="0">
                        <el-form-item label="打印机名称" prop="printerName">
                            <el-input v-model="form.printerName" placeholder="请输入打印机名称" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12" :offset="0">
                        <el-form-item label="打印机类型" prop="printerType">
                            <el-select v-model="form.printerType" @change="printerTypeChange" value-key=""
                                placeholder="请选择打印机类型" clearable filterable>
                                <el-option label="WIFI" value="1"></el-option>
                                <el-option label="4G" value="2"></el-option>
                            </el-select>

                        </el-form-item>
                    </el-col>
                    <el-col :span="12" :offset="0">
                        <el-form-item label="打印机卡号" prop="printerCard">
                            <el-input v-model="form.printerCard" placeholder="请输入打印机卡号" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12" :offset="0">
                        <el-form-item label="打印机品牌" prop="printerBrand">
                            <el-input v-model="form.printerBrand" placeholder="请输入打印机品牌" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12" :offset="0">
                        <el-form-item label="序列号SN" prop="serialNumberSn">
                            <el-input v-model="form.serialNumberSn" placeholder="请输入序列号SN" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12" :offset="0">
                        <el-form-item label="密钥KEY" prop="secretKey">
                            <el-input v-model="form.secretKey" placeholder="请输入密钥KEY" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12" :offset="0">
                        <el-form-item label="打印机型号" prop="printerModel">
                            <el-input v-model="form.printerModel" placeholder="请输入打印机型号" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12" :offset="0">
                        <el-form-item label="授权用户" prop="authorizedUserId">
                            <el-select v-model="form.authorizedUserId" value-key="" placeholder="请选择授权用户" clearable
                                filterable>
                                <el-option v-for="item in options" :key="item.value" :label="item.label"
                                    :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12" :offset="0">
                        <el-form-item label="生产商" prop="manufacturer">
                            <el-input v-model="form.manufacturer" placeholder="请输入生产商" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12" :offset="0">
                        <el-form-item label="厂家联系人" prop="manufacturerContact">
                            <el-input v-model="form.manufacturerContact" placeholder="请输入厂家联系人" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12" :offset="0">
                        <el-form-item label="厂家电话" prop="manufacturerPhone">
                            <el-input v-model="form.manufacturerPhone" placeholder="请输入厂家电话" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12" :offset="0">
                        <el-form-item label="启用状态" prop="activationStatus">
                            <el-switch v-model="form.activationStatus" :active-value="0" :inactive-value="1">
                            </el-switch>

                        </el-form-item>
                    </el-col>
                    <el-col :span="24" :offset="0">
                        <el-form-item label="备注">
                            <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
                        </el-form-item>
                    </el-col>



                </el-row>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>
  
<script setup name="PrinterInfo" lang="ts">
import { listPrinterInfo, getPrinterInfo, delPrinterInfo, addPrinterInfo, updatePrinterInfo } from '@/api/goods/printerInfo';
import { PrinterInfoVO, PrinterInfoQuery, PrinterInfoForm } from '@/api/goods/printerInfo/types';
import { getUserAllList } from '@/api/system/user';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const printerInfoList = ref<PrinterInfoVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const options = reactive([])
const queryFormRef = ref<ElFormInstance>();
const printerInfoFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
    visible: false,
    title: ''
});

const initFormData: PrinterInfoForm = {
    id: undefined,
    serialNumberSn: undefined,
    secretKey: undefined,
    printerName: undefined,
    printerModel: undefined,
    authorizedUser: undefined,
    printerBrand: undefined,
    manufacturer: undefined,
    manufacturerContact: undefined,
    manufacturerPhone: undefined,
    activationStatus: undefined,
    remark: undefined,
}
const data = reactive<PageData<PrinterInfoForm, PrinterInfoQuery>>({
    form: { ...initFormData },
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        serialNumberSn: undefined,
        secretKey: undefined,
        printerName: undefined,
        printerModel: undefined,
        nickName: undefined,
        authorizedUser: undefined,
        printerBrand: undefined,
        manufacturer: undefined,
        manufacturerContact: undefined,
        manufacturerPhone: undefined,
        activationStatus: undefined,
        params: {
        }
    },
    rules: {

        serialNumberSn: [
            { required: true, message: "序列号SN不能为空", trigger: "blur" }
        ],
        secretKey: [
            { required: true, message: "密钥KEY不能为空", trigger: "blur" }
        ],
        printerName: [
            { required: true, message: "打印机名称不能为空", trigger: "blur" }
        ],
        printerType: [
            { required: true, message: "打印机类型不能为空", trigger: "blur" }
        ],
        printerCard: [
            { required: true, message: "打印机卡号不能为空", trigger: "blur" }
        ],

        printerModel: [
            { required: true, message: "打印机型号不能为空", trigger: "blur" }
        ],
        authorizedUserId: [
            { required: true, message: "授权用户id不能为空", trigger: "blur" }
        ],
        printerBrand: [
            { required: true, message: "打印机品牌不能为空", trigger: "blur" }
        ],
        remark: [
            { required: true, message: "备注不能为空", trigger: "blur" }
        ],

    }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询打印机信息列表 */
const getList = async () => {
    loading.value = true;
    const res = await listPrinterInfo(queryParams.value);
    printerInfoList.value = res.rows;
    total.value = res.total;
    loading.value = false;
}

/** 取消按钮 */
const cancel = () => {
    reset();
    dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
    form.value = { ...initFormData };
    printerInfoFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
    queryParams.value.pageNum = 1;
    getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
    queryFormRef.value?.resetFields();
    handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: PrinterInfoVO[]) => {
    ids.value = selection.map(item => item.id);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
}

/** 新增按钮操作 */
const handleAdd = () => {
    reset();
    dialog.visible = true;
    dialog.title = "添加打印机信息";
}

/** 修改按钮操作 */
const handleUpdate = async (row?: PrinterInfoVO) => {
    reset();
    const _id = row?.id || ids.value[0]
    const res = await getPrinterInfo(_id);
    Object.assign(form.value, res.data);
    dialog.visible = true;
    dialog.title = "修改打印机信息";
}

/** 提交按钮 */
const submitForm = () => {
    printerInfoFormRef.value?.validate(async (valid: boolean) => {
        if (valid) {
            buttonLoading.value = true;
            if (form.value.id) {
                await updatePrinterInfo(form.value).finally(() => buttonLoading.value = false);
            } else {
                await addPrinterInfo(form.value).finally(() => buttonLoading.value = false);
            }
            proxy?.$modal.msgSuccess("操作成功");
            dialog.visible = false;
            await getList();
        }
    });
}

/** 删除按钮操作 */
const handleDelete = async (row?: PrinterInfoVO) => {
    const _ids = row?.id || ids.value;
    await proxy?.$modal.confirm('是否确认删除打印机信息？').finally(() => loading.value = false);
    await delPrinterInfo(_ids);
    proxy?.$modal.msgSuccess("删除成功");
    await getList();
}

/** 导出按钮操作 */
const handleExport = () => {
    proxy?.download('order/printerInfo/export', {
        ...queryParams.value
    }, `printerInfo_${new Date().getTime()}.xlsx`)
}
const getAllList = async () => {
    getUserAllList().then((res: any) => {
        options.push(...res.map(item => {
            return {
                value: item.userId,
                label: item.realityName
            }
        }))
    })
}
const statusChange = (row: PrinterInfoVO) => {
    updatePrinterInfo(row).then(() => {
        proxy?.$modal.msgSuccess("操作成功");
        getList();
    })
}
const parseJson = (data) => {
    let obj: any = JSON.stringify(data);
    return obj && obj.ret === 0 && obj.data || ''
}
// 
const printerTypeChange = (val) => {
    if (val == 1) {
        rules.value.printerCard[0].required = false;
    } else {
        rules.value.printerCard = [
            { required: true, message: "打印机卡号不能为空", trigger: "blur" }
        ]
    }

}
onMounted(() => {
    getList();
    getAllList();
});
</script>